package cn.lhn.ridersclub.controller;

import cn.lhn.ridersclub.anno.TokenVerify;
import cn.lhn.ridersclub.dto.AccountCardDto;
import cn.lhn.ridersclub.dto.ClubDto;
import cn.lhn.ridersclub.dto.ClubItemDto;
import cn.lhn.ridersclub.dto.Result;
import cn.lhn.ridersclub.entity.Account;
import cn.lhn.ridersclub.entity.Club;
import cn.lhn.ridersclub.entity.ClubClassify;
import cn.lhn.ridersclub.entity.ClubFollow;
import cn.lhn.ridersclub.service.ClubClassifyService;
import cn.lhn.ridersclub.service.ClubFollowService;
import cn.lhn.ridersclub.service.ClubService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/club")
public class ClubController {

    private final ClubService clubService;

    private final ClubFollowService clubFollowService;

    private final ClubClassifyService clubClassifyService;

    public ClubController(ClubService clubService, ClubFollowService clubFollowService, ClubClassifyService clubClassifyService) {
        this.clubService = clubService;
        this.clubFollowService = clubFollowService;
        this.clubClassifyService = clubClassifyService;
    }

    @GetMapping("/getById")
    public Result<ClubDto> getClubById(Long clubId) {
        ClubDto club = clubService.getClub(clubId);
        return Result.success("查询成功", club);
    }

    @GetMapping("/getByName")
    public Result<List<ClubDto>> getClubByName(String name) {
        List<ClubDto> list = clubService.getClub(name);
        return Result.success("查询成功", list);
    }

    @GetMapping("/getByClassify")
    public Result<List<ClubDto>> getClubByClassify(String classify) {
        return Result.success("查询成功", clubService.getByClassify(classify));
    }

    @GetMapping("/all")
    public Result<List<ClubDto>> getAllClubs() {
        List<ClubDto> clubs = clubService.getClubs();
        return Result.success("查询成功", clubs);
    }

    @GetMapping("/get/follow")
    public Result<List<ClubDto>> getFollowClubs(Long accountId) {
        List<ClubFollow> follows = clubFollowService.getFollows(accountId);
        List<ClubDto> list = new ArrayList<>();
        for (ClubFollow follow : follows) {
            list.add(getClubById(follow.getClubId()).getData());
        }
        return Result.success("查询成功", list);
    }

    @PostMapping("/add/follow")
    @TokenVerify
    public Result<?> addFollow(Long clubId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        clubFollowService.addFollow(clubId, Long.parseLong(accountId));
        FlushController.clubMap.put(Long.parseLong(accountId), true);
        return Result.success().message("添加成功");
    }

    @PostMapping("/delete/follow")
    @TokenVerify
    public Result<?> deleteFollow(Long clubId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        clubFollowService.deleteFollow(clubId, Long.parseLong(accountId));
        FlushController.clubMap.put(Long.parseLong(accountId), true);
        return Result.success().message("删除成功");
    }

    @GetMapping("/isFollow")
    @TokenVerify
    public Result<Boolean> isFollow(Long clubId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        Boolean follow = clubFollowService.isFollow(clubId, Long.parseLong(accountId));
        return Result.success("查询成功", follow);
    }

    @GetMapping("/fansCount")
    public Result<Long> getFansCount(Long clubId) {
        Long fansCount = clubFollowService.getFansCount(clubId);
        return Result.success("查询成功", fansCount);
    }

    @GetMapping("/classify/all")
    public Result<List<ClubClassify>> getAllClassify() {
        return Result.success("查询成功", clubClassifyService.getAllClassify());
    }

    @GetMapping("/listFans")
    public Result<List<AccountCardDto>> listFans(Long clubId) {
        List<AccountCardDto> list = clubFollowService.listFans(clubId);
        return Result.success(list);
    }

    @PostMapping("/addWant")
    @TokenVerify
    public Result<?> addWant(Long clubId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        clubService.addWant(clubId, Long.parseLong(accountId));
        return Result.success();
    }

    @PostMapping("/deleteWant")
    @TokenVerify
    public Result<?> deleteWant(Long clubId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        clubService.deleteWant(clubId, Long.parseLong(accountId));
        return Result.success();
    }

    @GetMapping("/isWant")
    @TokenVerify
    public Result<Boolean> isWant(Long clubId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        Boolean want = clubService.isWant(clubId, Long.parseLong(accountId));
        return Result.success(want);
    }

    @PostMapping("/postGrade")
    @TokenVerify
    public Result<?> postGrade(Long clubId, Long grade, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        clubService.postGrade(clubId, Long.parseLong(accountId), grade);
        return Result.success();
    }

    @GetMapping("/isPostGrade")
    @TokenVerify
    public Result<Boolean> isPostGrade(Long clubId, HttpServletRequest request) {
        String accountId = request.getHeader("arg0");
        Boolean isPost = clubService.isPostGrade(clubId, Long.parseLong(accountId));
        return Result.success(isPost);
    }

    @GetMapping("/list3FansId")
    public Result<List<Long>> list3FansId(Long clubId) {
        List<Long> list = clubService.list3FansId(clubId);
        return Result.success(list);
    }

    @GetMapping("/listBannerClubId")
    public Result<List<Long>> listBannerClubId() {
        return Result.success(clubService.listBannerClubId());
    }

    @GetMapping("/listClubByClassify")
    public Result<List<ClubItemDto>> listClubByClassify(String classify) {
        List<ClubItemDto> list = clubService.listClubByClassify(classify);
        return Result.success(list);
    }
}
